﻿using SuperSocket.SocketBase;
using SuperSocket.SocketEngine;
using superSocketDemo.Client;
using superSocketDemo.info;
using superSocketDemo.Server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace superSocketDemo
{
    /// <summary>
    /// socket服务管理
    /// </summary>
    public  class SocketManager
    {

        /// <summary>
        /// 初始化
        /// </summary>
        public static void init()
        {
            //var bootstrap = BootstrapFactory.CreateBootstrap();

            //if (!bootstrap.Initialize())
            //{
            //    Console.WriteLine("初始化失败!");
            //    Console.ReadKey();
            //    return;
            //}


            //修改appserver为myserver
            var appServer = new SocketServer();
            //注册事件
             appServer.NewSessionConnected += new SessionHandler<SocketClientSession>(OnNewSessionConnected);
            appServer.NewRequestReceived += new RequestHandler<SocketClientSession, MyRequestInfo>(OnNewRequestReceived);

            //设置端口号
            int port = 6679;
            //启动应用服务端口
            if (!appServer.Setup(port)) //启动时监听端口2017
            {
                Console.WriteLine("服务端口启动失败!");
                Console.ReadKey();
                return;
            }

            Console.WriteLine();

            //尝试启动应用服务
            if (!appServer.Start())
            {
                Console.WriteLine("服务启动失败!");
                Console.ReadKey();
                return;
            }

            Console.WriteLine("The server started successfully, press key 'q' to stop it!");

            while (Console.ReadKey().KeyChar != 'q')
            {
                Console.WriteLine();
                continue;
            }

            Console.WriteLine();
            //Stop the appServer

            appServer.Stop();


            //var result = bootstrap.Start();

            //Console.WriteLine("服务正在启动: {0}!", result);

            //if (result == StartResult.Failed)
            //{
            //    Console.WriteLine("服务启动失败!");
            //    Console.ReadKey();
            //    return;
            //}
            //Console.WriteLine("服务启动成功，请按'E'停止服务!");

            //while (Console.ReadKey().KeyChar != 'E')
            //{
            //    Console.WriteLine();
            //    continue;
            //}

            ////停止服务
            //// appServer.Stop();
            //bootstrap.Stop();
            //Console.WriteLine("服务已停止!");
            //Console.ReadKey();

        }

        static void OnNewRequestReceived(SocketClientSession session, MyRequestInfo requestInfo)
        {
            Console.WriteLine("Received something");
            //ByteBuffer buffer = new ByteBuffer(requestInfo.Body);
            //var monster = Monster.GetRootAsMonster(buffer);
            //Console.WriteLine("HP: {0}", monster.Hp);
            //Console.WriteLine("Mana: {0}", monster.Mana);
            //Console.WriteLine("Color: {0}", monster.Color);
            //Console.WriteLine("Pos: ({0}, {1}, {2})", monster.Pos.Value.X, monster.Pos.Value.Y, monster.Pos.Value.Z);
            //Console.WriteLine("Weapon: {0}", monster.Weapons(0).Value.Name);
        }

        static void OnNewSessionConnected(SocketClientSession session)
        {
            Console.WriteLine("Client Accepted");
           // session.Send("Welcome", null);
        }

    }
}
